package edu.ztr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import edu.ztr.mapper.SearchMapper;
import edu.ztr.pojo.Actor;
import edu.ztr.pojo.Ele;
import edu.ztr.pojo.PageBean;
import edu.ztr.service.SearchService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class SearchServiceImp implements SearchService {
    @Autowired
    private SearchMapper searchMapper;

//    @Override
//    public List<Ele> list() {
//        return searchMapper.list();
//    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String content, LocalDate begin, LocalDate end) {
        // 1.设置分页参数
        PageHelper.startPage(page, pageSize);

        // 2.执行操作
        List<Ele> EleList = searchMapper.list(content, begin, end);
        Page<Ele> p = (Page<Ele>) EleList; // 强制转换为page类

        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public PageBean getMovie(Integer page, Integer pageSize, String name) {
        // 1.设置分页参数
        PageHelper.startPage(page, pageSize);
        // 2.执行操作
        List<Ele> EleList = searchMapper.getMovie(name);
        Page<Ele> p = (Page<Ele>) EleList; // 强制转换为page类
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public List<Actor> getActor(String id) {
        return searchMapper.getActor(Integer.parseInt(id));
    }

    @Override
    public List<String> getApi() {
        return searchMapper.getApi();
    }

    @Override
    public boolean deleteMovie(Integer id) {
        return searchMapper.deleteMovie(id) != 0;
    }

    @Override
    public boolean idIn(Integer id) {
        return searchMapper.idIn(id) != 0;
    }


    @Override
    public boolean edit(Integer id, LocalDate time, String content, String href, Integer way) {
        if (way == 0) {
            return searchMapper.updateData(id, time, content, href) != 0;
        } else {
            if (idIn(id)) {
                return false;
            } else {
                return searchMapper.addData(id, time, content, href) != 0;
            }
        }
    }

    @Override
    public boolean deleteSearch(Integer id) {
        return searchMapper.deleteSearch(id) != 0;
    }


}
